<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <!-- card title -->
        <div slot="header">
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>{{ company.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="openAddDept()">
                        添加顶级部门
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </div>
        <!-- card body -->
        <el-tree :data="treeData" :props="defaultProps" default-expand-all>
          <!--
            1. 默认插槽形式=》自定义每个节点的html结构
            2. 作用域插槽=》通过data获取节点数据
            注意：自定义结构只能有一个根元素
           -->
          <template #default="{ data }">
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager || "--" }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="openAddDept(data,1)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="openAddDept(data,2)">
                          编辑部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="delDept(data)">
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!--  -->
    <AddDept ref="ad" :all-depts="allDepts" :parent-dept="parentDept" :show-dialog="showDialog" @refresh="getTreeData" @close-dialog="showDialog=$event"></AddDept>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments.js'
import { transformTreeData } from '@/utils/index.js'
// 导入新增部门弹层
import AddDept from '@/views/departments/components/add-dept.vue'
export default {
  components: {
    AddDept
  },
  data () {
    return {
      // 原始数据
      allDepts: [],
      // 公司部门数据（树形结构）
      treeData: [],
      company: { name: '' },
      // 需求：后台返回的数据部门名叫name
      defaultProps: {
        children: 'children',
        label: 'name' // 指定渲染使用的部门属性名叫name
      },
      showDialog: false,
      parentDept: null
    }
  },
  created () {
    this.getTreeData()
  },
  methods: {
    // 获取部门数据
    async getTreeData () {
      const { depts, companyName } = await getDepartments()
      // console.table(depts)
      // console.log(transformTreeData(depts))
      this.allDepts = depts
      this.treeData = transformTreeData(depts)
      this.company.name = companyName
    },
    // 删除部门
    async delDept (curDept) {
      // console.log(curDept)
      try {
        await this.$confirm(`确认删除${curDept.name}嘛`, '提示')
        if (curDept.children && curDept.children.length > 0) {
          return this.$message.error('不能直接删除父部门')
        }
        // 删除
        await delDepartments(curDept.id)
        // 刷新列表
        this.getTreeData()
        this.$message.success('删除成功')
      } catch (error) {
        return this.$message.error('删除失败')
      }
    },
    // 打开新增部门弹层
    // parentDept 添加子部门的父部门数据 =》说明：顶级部门没有父，所以parentDept 是undefined
    openAddDept (parentDept, type) {
      // 存储子部门的父部门
      this.parentDept = parentDept
      // 2.打开弹层
      this.showDialog = true
      console.log(type)
      if (type === 2) {
        // 编辑状态
        // this.$refs.ad 弹层组件实例
        this.$refs.ad.getDeptDetail(parentDept.id)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
